package com.tyg.controller;


import com.tyg.entity.IsVip;
import com.tyg.service.IsVipService;
import com.tyg.util.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;

/**
 * <p>
 * 顾客表 前端控制器
 * </p>
 *
 * @author zhangjingnan
 * @since 2022-10-22
 */
@CrossOrigin
@RestController
@RequestMapping("/is-vip")
@Slf4j
public class IsVipController {

    @Autowired
    IsVipService isVipService;

    @PostMapping("/getIsVipById")
    public ResultCode getIsVipById(Integer workId){
        try{
            if(ObjectUtils.isEmpty(isVipService.selectByWorkId(workId))){
                System.out.println(workId);
                isVipService.insertNew(workId);
                IsVip isVip=isVipService.selectAll(workId);
                return ResultCode.success(isVip);
            }else{
                if(!ObjectUtils.isEmpty(workId)){
                    IsVip isVip=isVipService.selectAll(workId);
                    return ResultCode.success(isVip);
                }else{
                    return ResultCode.error(500, "查询失败！未获取到workId");
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
            return ResultCode.error(500, "查询失败！请联系服务人员");
        }
    }

    @PostMapping("/invest")
    public ResultCode invest(@Param("totalAmt") String totalAmt, @Param("workId") String workId){
        try {
            if (!ObjectUtils.isEmpty(totalAmt) && !ObjectUtils.isEmpty(workId)){
                IsVip isVip=isVipService.Invest(Double.parseDouble(totalAmt), Integer.parseInt(workId));
                return ResultCode.success();
            }else return ResultCode.error(500,"参数有误!");
        }catch (Exception e){
            e.printStackTrace();
            return ResultCode.error(500, "充值失败！请联系服务人员");
        }
    }

}

